using System;
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using DotNetNuke.Entities.Tabs;
using System.Collections.Generic;

namespace HeroldIT.Dnn.SkinObjects
{
    /// <summary>
    /// A SkinObject for generating a page's title.
    /// </summary>
    [DefaultProperty("Prefix")]
    public class PageTitleGenerator : BreadcrumbBase
    {
        #region static methods

        /// <summary>
        /// Gets the <see cref="PageTitleGenerator"/> control for the page.
        /// </summary>
        /// <param name="page">The page to look on.</param>
        /// <returns>The <see cref="PageTitleGenerator"/> control for the page, or <c>null</c> if none is present.</returns>
        public static PageTitleGenerator GetCurrent(Page page)
        {
            if (null == page)
                throw new ArgumentNullException("page");

            return page.Items[typeof (PageTitleGenerator)] as PageTitleGenerator;
        }

        #endregion

        #region properties

        private bool displayBreadcrumb = true;

        /// <summary>
        /// Whether to display the complete breadcrumb in the title or just the active tab. Default is <c>true</c>.
        /// </summary>
        [Browsable(true)]
        [Category("Behavior")]
        [DefaultValue(true)]
        [Localizable(false)]
        public bool DisplayBreadcrumb
        {
            get { return this.displayBreadcrumb; }
            set { this.displayBreadcrumb = value; }
        }

        #endregion

        #region method overrides

        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.Init" /> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs" /> object that contains the event data.</param>
        protected override void OnInit(EventArgs e)
        {
            if (null != this.Page && null == GetCurrent(this.Page))
            {
                this.Page.Items[typeof(PageTitleGenerator)] = this;
            }
            else
            {
                throw new InvalidOperationException("Only one PageTitleGenerator is allowed on a page.");
            }

            base.OnInit(e);
        }

        /// <summary>
        /// Is called before the control is rendered.
        /// </summary>
        /// <param name="e">The <see cref="EventArgs"/> passed to the event.</param>
        protected override void OnPreRender(EventArgs e)
        {
            this.Page.Header.Title = this.GenerateTitle();

            base.OnPreRender(e);
        }

        #endregion

        #region methods

        private string GenerateTitle()
        {
            var title = new StringBuilder();

            if (this.PortalSettings.ActiveTab.TabID.Equals(this.PortalSettings.HomeTabId))
            {
                switch (this.PrefixType)
                {
                    case PrefixType.HomeTabName:
                        if (this.UsePageLocalization)
                        {
                            return Localizer.LocalizeTab(this.HomeTab).TabName;
                        }
                        return this.HomeTab.TabName;

                    case PrefixType.HomeTabTitle:
                        if (this.UsePageLocalization)
                        {
                            return this.GetTabTitle(Localizer.LocalizeTab(this.HomeTab));
                        }
                        return this.GetTabTitle(this.HomeTab);
                }
            }

            if (!this.ReverseDirection)
            {
                bool renderSeparator = false;
                string prefix = this.GeneratePrefix();
                if (!String.IsNullOrEmpty(prefix))
                {
                    title.Append(prefix);
                    renderSeparator = true;
                }

                if (this.DisplayBreadcrumb)
                {
                    List<string> parts = new List<string>();
                    foreach (TabInfo part in this.GetBreadcrumbPartsToDisplay())
                    {
                        BreadcrumbEventArgs e = this.GetRenderInfo(part);
                        if (!e.OmitBreadcrumbPart)
                        {
                            parts.Add(e.NameToRender);
                        }
                    }

                    var breadcrumb = String.Join(this.Separator, parts.ToArray());
                    if (title.Length > 0 && breadcrumb.Length > 0)
                    {
                        title.Append(this.Separator);
                        renderSeparator = true;
                    }
                    title.Append(breadcrumb);
                }

                BreadcrumbEventArgs bea = this.GetRenderInfo(this.UsePageLocalization ? Localizer.LocalizeTab(this.PortalSettings.ActiveTab) : this.PortalSettings.ActiveTab);
                if (!bea.OmitBreadcrumbPart)
                {
                    if (renderSeparator)
                    {
                        title.Append(this.Separator);
                    }
                    title.Append(bea.NameToRender);
                }
            }
            else
            {
                BreadcrumbEventArgs bea = this.GetRenderInfo(this.UsePageLocalization ? Localizer.LocalizeTab(this.PortalSettings.ActiveTab) : this.PortalSettings.ActiveTab);
                if (!bea.OmitBreadcrumbPart)
                {
                    title.Append(bea.NameToRender);
                }

                if (this.DisplayBreadcrumb)
                {
                    List<string> parts = new List<string>();

                    foreach (TabInfo part in this.GetBreadcrumbPartsToDisplay())
                    {
                        BreadcrumbEventArgs e = this.GetRenderInfo(part);
                        if (!e.OmitBreadcrumbPart)
                        {
                            parts.Insert(0, e.NameToRender);
                        }
                    }
                    
                    string breadcrumb = String.Join(this.Separator, parts.ToArray());
                    if (breadcrumb.Length > 0)
                    {
                        title.Append(this.Separator);
                        title.Append(breadcrumb);
                    }
                }

                string prefix = this.GeneratePrefix();
                if (!String.IsNullOrEmpty(prefix))
                {
                    title.Append(this.Separator);
                    title.Append(prefix);
                }
            }

            return title.ToString();
        }

        #endregion
    }
}
